package com.lanou.myapp.book.boybook;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.lanou.myapp.R;
import com.lanou.myapp.book.boybook.contentboybook.ContentBoyBookActivity;
import com.lanou.myapp.comic.recommendedcomic.ComicsBeans;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/15.
 */
public class RecyclerBoyBookAdapter extends RecyclerView.Adapter<RecyclerBoyBookAdapter.MyViewHolder>{
    private Context context;
    private List<BoyBook> data;



    public RecyclerBoyBookAdapter(Context context) {
        this.context = context;
//        init();
    }

//    private void init() {
//        queue = Volley.newRequestQueue(context);
//        lruCache = new LruCache<String, Bitmap>(20);
//        imageCache = new ImageLoader.ImageCache() {
//            @Override
//            public Bitmap getBitmap(String url) {
//                return lruCache.get(url);
//            }
//
//            @Override
//            public void putBitmap(String url, Bitmap bitmap) {
//                lruCache.put(url, bitmap);
//            }
//        };
//        //构建imageLoader对象
//        imageLoader = new ImageLoader(queue, imageCache);
//    }

//    public void getBitmapCache(String imgUrl, ImageView imageView) {
//        // 构建图片监听器
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.v6_boy_unseletor_icon,
//                R.mipmap.v6_boy_unseletor_icon);
//        // 图片拉取
//        imageLoader.get(imgUrl, listener, 280, 250);
//    }


    public void addData(List<BoyBook> data){
        this.data = data;
        notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder myViewHolder = null;
        View view = LayoutInflater.from(context).inflate(R.layout.item_boy_book,null);
        myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        holder.rl_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ContentBoyBookActivity.class);
                intent.putExtra("key", data.get(position).getData().get(0).getBookId());
                context.startActivity(intent);
            }
        });

        holder.rl_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ContentBoyBookActivity.class);
                intent.putExtra("key", data.get(position).getData().get(1).getBookId());
                context.startActivity(intent);
            }
        });

        holder.rl_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ContentBoyBookActivity.class);
                intent.putExtra("key", data.get(position).getData().get(2).getBookId());
                context.startActivity(intent);
            }
        });



        holder.tv_title.setText(data.get(position).getTitle());
        holder.tv_subtitle.setText(data.get(position).getSubtitle());

//        String imgurl = data.get(position).get;
//        String image_circle = data.get(position).getTopic().getUser().getAvatar_url();
//        getBitmapCache(imgurl, holder.imageView);
//        getBitmapCache(image_circle, holder.image_circle);


        holder.tv_name.setText(data.get(position).getData().get(0).getBookName());
        holder.tv_name2.setText(data.get(position).getData().get(1).getBookName());
        holder.tv_name3.setText(data.get(position).getData().get(2).getBookName());

        holder.tv_description.setText(data.get(position).getData().get(0).getDescription());
        holder.tv_description2.setText(data.get(position).getData().get(1).getDescription());
        holder.tv_description3.setText(data.get(position).getData().get(2).getDescription());

        holder.tv_author.setText(data.get(position).getData().get(0).getAuthor());
        holder.tv_author2.setText(data.get(position).getData().get(1).getAuthor());
        holder.tv_author3.setText(data.get(position).getData().get(2).getAuthor());

        holder.tv_type.setText(data.get(position).getData().get(0).getCategoryName());
        holder.tv_type2.setText(data.get(position).getData().get(1).getCategoryName());
        holder.tv_type3.setText(data.get(position).getData().get(2).getCategoryName());

        holder.tv_status.setText(data.get(position).getData().get(0).getBookStatus());
        holder.tv_status2.setText(data.get(position).getData().get(1).getBookStatus());
        holder.tv_status3.setText(data.get(position).getData().get(2).getBookStatus());

        holder.tv_num.setText(data.get(position).getData().get(0).getWordsCount() / 10000 + "万字");
        holder.tv_num2.setText(data.get(position).getData().get(1).getWordsCount() / 10000 + "万字");
        holder.tv_num3.setText(data.get(position).getData().get(2).getWordsCount() / 10000 + "万字");
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        private RelativeLayout rl_one, rl_two, rl_three;

        private TextView tv_title, tv_subtitle, tv_name, tv_description, tv_author, tv_type, tv_status, tv_num;

        private TextView tv_name2, tv_description2, tv_author2, tv_type2, tv_status2, tv_num2;

        private TextView tv_name3, tv_description3, tv_author3, tv_type3, tv_status3, tv_num3;

        public MyViewHolder(View itemView) {
            super(itemView);
            rl_one = (RelativeLayout) itemView.findViewById(R.id.relativeLayout1);
            rl_two = (RelativeLayout) itemView.findViewById(R.id.relativeLayout2);
            rl_three = (RelativeLayout) itemView.findViewById(R.id.relativeLayout3);

            tv_title = (TextView) itemView.findViewById(R.id.tv_title_boy_book);
            tv_subtitle = (TextView) itemView.findViewById(R.id.tv_subtitle_boy_book);

            tv_name = (TextView) itemView.findViewById(R.id.tv_name_boy_book);
            tv_name2 = (TextView) itemView.findViewById(R.id.tv_name2_boy_book);
            tv_name3 = (TextView) itemView.findViewById(R.id.tv_name3_boy_book);

            tv_description = (TextView) itemView.findViewById(R.id.tv_describe_boy_book);
            tv_description2 = (TextView) itemView.findViewById(R.id.tv_describe2_boy_book);
            tv_description3 = (TextView) itemView.findViewById(R.id.tv_describe3_boy_book);

            tv_author = (TextView) itemView.findViewById(R.id.tv_author_boy_book);
            tv_author2 = (TextView) itemView.findViewById(R.id.tv_author2_boy_book);
            tv_author3 = (TextView) itemView.findViewById(R.id.tv_author3_boy_book);

            tv_type = (TextView) itemView.findViewById(R.id.tv_type_boy_book);
            tv_type2 = (TextView) itemView.findViewById(R.id.tv_type2_boy_book);
            tv_type3 = (TextView) itemView.findViewById(R.id.tv_type3_boy_book);

            tv_status = (TextView) itemView.findViewById(R.id.tv_status_boy_book);
            tv_status2 = (TextView) itemView.findViewById(R.id.tv_status2_boy_book);
            tv_status3 = (TextView) itemView.findViewById(R.id.tv_status3_boy_book);

            tv_num = (TextView) itemView.findViewById(R.id.tv_num_boy_book);
            tv_num2 = (TextView) itemView.findViewById(R.id.tv_num2_boy_book);
            tv_num3 = (TextView) itemView.findViewById(R.id.tv_num3_boy_book);
        }
    }
}
